#pragma once

namespace micro
{
namespace gui
{


class QImageList
{
protected:

    HIMAGELIST m_Object;
public:

    QImageList(): m_Object( nullptr ) {}
    QImageList( HIMAGELIST hima )
    {
        m_Object = hima;
    }
    ~QImageList()
    {
        Destroy();
    }

    operator HIMAGELIST() const
    {
        return m_Object;
    }

    bool Attach( HIMAGELIST imageList )
    {
        if ( imageList == nullptr )
            return false;

        m_Object = imageList;
        return true;
    }

    HIMAGELIST Detach()
    {
        HIMAGELIST imageList = m_Object;
        m_Object = nullptr;
        return imageList;
    }

    bool Create( int width, int height, int initialNumber, int grow = 1,  UINT flags = ILC_COLOR32 )
    {
        HIMAGELIST a = ImageList_Create( width, height, flags,
                                         initialNumber, grow );

        if ( a == nullptr )
            return false;

        return Attach( a );
    }

    bool Destroy() // DeleteImageList() in MFC
    {
        if ( m_Object == nullptr )
            return false;

        return toBool( ImageList_Destroy( Detach() ) );
    }

    inline int GetImageCount() const
    {
        return ImageList_GetImageCount( m_Object );
    }

    bool GetImageInfo( int index, IMAGEINFO* imageInfo ) const
    {
        return toBool( ImageList_GetImageInfo( m_Object, index, imageInfo ) );
    }

    int AddBitmap( HBITMAP hbmImage, HBITMAP hbmMask = nullptr )
    {
        return ImageList_Add( m_Object, hbmImage, hbmMask );
    }
    int AddMasked( HBITMAP hbmImage, COLORREF mask )
    {
        return ImageList_AddMasked( m_Object, hbmImage, mask );
    }
    int AddIcon( HICON icon )
    {
        return ImageList_AddIcon( m_Object, icon );
    }
    int Replace( int index, HICON icon )
    {
        return ImageList_ReplaceIcon( m_Object, index, icon );
    }

    // If index is -1, the function removes all images.
    bool Remove( int index )
    {
        return toBool( ImageList_Remove( m_Object, index ) );
    }
    bool RemoveAll()
    {
        return toBool( ImageList_RemoveAll( m_Object ) );
    }

    HICON ExtractIcon( int index )
    {
        return ImageList_ExtractIcon( nullptr, m_Object, index );
    }

    HICON GetIcon( int index, UINT flags )
    {
        return ImageList_GetIcon( m_Object, index, flags );
    }

    bool GetIconSize( int& width, int& height ) const
    {
        return toBool( ImageList_GetIconSize( m_Object, &width, &height ) );
    }

    bool SetIconSize( int width, int height )
    {
        return toBool( ImageList_SetIconSize( m_Object, width, height ) );
    }




    void Draw( HDC h, int nr, int x, int y, UINT fStyle  = ILD_NORMAL )
    {
        ASSERT ( nr >=0 && nr < GetImageCount() );
        BOOL bo = ::ImageList_Draw( m_Object, nr, h,   x,   y,  fStyle );
		if(!bo)
		{
			QError e;
			e.PrintMessage();
		}
    }
    void SetOverlayImage( int zerobased, int onebased )
    {
        ::ImageList_SetOverlayImage ( m_Object,
                                      zerobased,     // 0-based index to image list.
                                      onebased );   // 1-based index to overlay mask
    }






    //WINCOMMCTRLAPI HIMAGELIST  WINAPI ImageList_Read(_In_ struct IStream *pstm);
    //WINCOMMCTRLAPI BOOL        WINAPI ImageList_Write(_In_ HIMAGELIST himl, _In_ struct IStream *pstm);




};




}// gui

}//micro